extends CharacterBody2D


@export var move_speed : float
@export var animator : AnimatedSprite2D
@export var bullet_scene : PackedScene

var is_game_over : bool = false

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	get_tree().current_scene.score = 0
	print("我TM来啦！ -_-")

func _process(delta: float) -> void:
	if velocity == Vector2.ZERO or is_game_over: # 走路音乐监听逻辑
		$Running.stop()
	elif not $Running.playing:
		$Running.play()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
	velocity = move_speed*Input.get_vector("left", "right", "up", "down")
	
	if not is_game_over:
		# play run animation if run
		if velocity == Vector2.ZERO:
			animator.play("idle")
		else:
			animator.play("run")	
		move_and_slide()
	
func game_over():
	if not is_game_over:
		is_game_over = true
		animator.play("game_over")
		$GameOver.play()
		get_tree().current_scene.show_game_over()
		
		# 重启游戏
		$ReloadTimer.start()
		
func _on_fire() -> void:
	if is_game_over or velocity != Vector2.ZERO:
		return
	var bullet_node = bullet_scene.instantiate()
	bullet_node.position = position + Vector2(7,6)
	get_tree().current_scene.add_child(bullet_node)
	$Gun.play()
	print("开炮！！！")

func _reload_scene():
	get_tree().reload_current_scene() #绑定了ReloadTimer
